<template>
  <div>
     <el-container>
      <div class="el_submit" style="text-align:right;padding-right:20px">
        <el-button type="primary" @click="submit(turnForm)" style="margin-bottom:20px" :disabled= isAllow>提交</el-button>
      </div>
      <el-header class="el_header" style="height:280px">
        <el-form :inline="true" :model="turnForm">
          <div style="margin-top: 20px;">
            <el-form-item label="开发申请号" :label-width="formLabelWidth">
              <el-input v-model="turnForm.dlvApply.applyNo" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="采购组织" :label-width="formLabelWidth">
              <el-select v-model="turnForm.dlvApply.purOrgId" placeholder="请选择" @change="handleChange">
                <el-option v-for="item in purOrgOptions" :key="item.id" :label="item.purOrgName" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="采购组" :label-width="formLabelWidth">
              <el-select v-model="turnForm.dlvApply.purGroupId" placeholder="请选择">
                <el-option v-for="item in relationPurGroup" :key="item.id" :label="item.purGroupName" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="供应商编码" :label-width="formLabelWidth">
              <el-input v-model="turnForm.vendorCode" readonly="true"></el-input>
            </el-form-item>
            <el-form-item label="供应商" :label-width="formLabelWidth">
              <select-table v-model="turnForm.vendorName" @on-selection-data="getRowData"></select-table>
            </el-form-item>
            <el-form-item label="引入类型" :label-width="formLabelWidth">
              <el-select v-model="turnForm.applyType" placeholder="请选择">
                <el-option v-for="item in applyTypeOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="品类" :label-width="formLabelWidth">
               <select-tree :multiple="false" ref="selectTree" v-model="turnForm.dlvApply.categoryName"></select-tree>
            </el-form-item>
            <el-form-item label="申请主题" :label-width="formLabelWidth">
              <el-input v-model="turnForm.dlvApply.title" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="申请原因" :label-width="formLabelWidth">
              <el-input v-model="turnForm.dlvApply.applyReason" auto-complete="off"></el-input>
            </el-form-item>
          </div>
          <el-breadcrumb separator="/" class="submenu">
            <el-breadcrumb-item class="main-top">SAP创建补充资料</el-breadcrumb-item>
          </el-breadcrumb>
          <div style="margin-top: 20px;">
            <el-form-item label="检索项1" :label-width="formLabelWidth">
              <el-input v-model="turnForm.nameSort" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="统驭科目" :label-width="formLabelWidth">
              <el-select v-model="turnForm.dictItemId" placeholder="请选择">
                <el-option v-for="item in dictItemIdOptions" :key="item.id" :label="item.remarks" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="付款方式" :label-width="formLabelWidth">
              <el-select v-model="turnForm.dictItemId2" placeholder="请选择">
                <el-option v-for="item in dictItemId2Options" :key="item.id" :label="item.remarks" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="付款条件" :label-width="formLabelWidth">
            <el-select v-model="turnForm.dictItemId3" placeholder="请选择">
                <el-option v-for="item in dictItemId3Options" :key="item.id" :label="item.remarks" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </div>
        </el-form>
      </el-header>
    </el-container>
  </div>
</template>
<script>
  import SelectTable from '@/components/common/select-ext/select-table';
  import SelectTree from '@/components/common/select-ext/select-tree.vue';
  export default {
    components: {
      SelectTable,
      SelectTree
    },
    data() {
      return {
        isAllow: false,
        dlvApplyItemId: this.$route.query.dlvApplyItemId,
         // 是否显示隐藏列
        isShow:false,
         // 对话框列宽
        formLabelWidth: '120px',
        turnForm: {
          vendorCode:'',
          applyType:'',
          nameSort:'',
          dictItemId:'',
          dictItemId2:'',
          dictItemId3:'',
          dlvApply:{},
        },
         // 引入类型
        applyTypeOptions: [{
            value: 1,
            label: '新供应商'
          },
          {
            value: 2,
            label: '合格供应商-新物料开发'
          }
        ],
        // 采购组织下拉
        purOrgOptions: [],
        // 采购组下拉
        relationPurGroup: [],
        // 统驭科目下拉
        dictItemIdOptions: [],
        // 付款方式
        dictItemId2Options: [],
        // 付款条件下拉
        dictItemId3Options: []
      };
    },
    mounted() {
      this.initData();
      this.getDlvApplyItem();
    },
     // 监听品类下拉
    watch: {
      'turnForm.categoryName':function(val){
         let data = this.$refs['selectTree'].getResultData();
         this.turnForm.dlvApply.categoryCode = data.categoryCode;
      }
    },
    methods: {
      initData(){
        // 获取采购组织
        this.getSelectData('get', this.lib.config.host.masterData + '/system/purOrgMgt/purOrg/getPurOrgByUserId/' + this.getCurrUserId(), {}, 'id', 'purOrgName').then((data)=>{
          this.purOrgOptions = data;
        });
          // 获取srm采购组
        this.getSelectData('post', this.lib.config.host.masterData + '/mdata/purOrgMgt/purGroup/getByUserId/' + this.getCurrUserId(), {}, 'id', 'purGroupName').then((data)=>{
          this.relationPurGroup = data;
        });
        // 查询付款方式数据字典
        this.getDicData('PAYMENT_TERMS').then((data)=>{
          this.dictItemId2Options = data;
        });
        // 查询付款条件数据字典
        this.getDicData('PAYMENT_TYPE').then((data)=>{
          this.dictItemId3Options = data;
        });
        // 查询统驭科目数据字典
        this.getDicData('RECONCILIATION_ACCOUNT').then((data)=>{
          this.dictItemIdOptions = data;
        });
      },
      // 采购组织在改变的时候查询采购组
      handleChange(value) {
        // 获取srm采购组
        this.getSelectData('post', this.lib.config.host.masterData + '/mdata/purOrgMgt/purGroup/getByPurOrgId/' + value, {}, 'id', 'purGroupName').then((data)=>{
          this.relationPurGroup = data;
        });
      },   
       // 查询供应商开发申请明细
      getDlvApplyItem() {
         this.axios.post(this.lib.config.host.vendorDlv + "/vendormgt/dlv/dlvApply/findDlvApplyItem/" + this.dlvApplyItemId)
          .then((response) => {
            delete response.data["deleteFlag"];  
            delete response.data["createTime"]; 
            delete response.data["createUserId"]; 
            delete response.data["id"]; 
            delete response.data["lastModifyTime"]; 
            delete response.data["lastModifyUserId"]; 
            delete response.data["publishStatus"]; 
            this.turnForm = response.data;
          })
          .catch((error) => {
            console.log(error);
          });
      },
      // 保存
      submit(turnForm) {
        this.axios.post(this.lib.config.host.vendorDlv + '/vendormgt/build/buildSaveAndPublish', turnForm)
         .then((response) =>  {
           if(response.data.success){
             this.isAllow = true;
             this.$message({
                showClose: true,
                message: '操作成功！',
                type: 'info'
              });
           } else {
              this.$message({
                showClose: true,
                message: response.data.message,
                type: 'warning'
              });
           }
          })
          .catch((error) => {
            this.$message({
              showClose: true,
              message: error.response.data.message,
              type: 'warning'
            });
          });
      },
        // 双击供应商触发
      getRowData(data){
        this.turnForm.vendorCode = data.vendorCode;
      },
    }
  }
</script>